#!/bin/bash


# client
docker stack deploy -c node-exporter-cadvisor.yaml monitor
docker service ls --filter name=monitor
docker service rm monitor_node-exporter monitor_cadvisor

# server
docker config rm monitor_prometheus-config
docker stack deploy -c prometheus-grafana.yml monitor
docker service ls --filter name=monitor
docker service rm monitor_prometheus monitor_grafana

## dashboard
#导入 左边+按钮 -> Import -> 8919 -> prometheus选择源 -> Import

# node_exporter
# https://grafana.com/grafana/dashboards/9276
# https://grafana.com/grafana/dashboards/8919

# cadvisor
# https://grafana.com/grafana/dashboards/193

# wmi_exporter
# https://grafana.com/grafana/dashboards/10467

# other
# https://grafana.com/orgs/starsliao


cat > /tmp/monitor.sh <<EOF
# 监控 linux
docker rm -f node-exporter
docker run -d -p 9100:9100 \
  --name node-exporter \
  -h $(hostname) \
  -v "/proc:/host/proc:ro" \
  -v "/sys:/host/sys:ro" \
  -v "/:/rootfs:ro" \
  --net="host" \
  --pid="host" \
  --cap-add=SYS_TIME \
  --cpus 0.1 \
  --memory 32M \
  --restart always \
  prom/node-exporter \
    --path.rootfs /rootfs \
    --path.procfs /host/proc \
    --path.sysfs /host/sys \
    --collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)"

# 监控容器
docker rm -f cadvisor
docker run \
  -e TZ=Asia/Shanghai \
  --volume=/:/rootfs:ro \
  --volume=/var/run:/var/run:ro \
  --volume=/sys:/sys:ro \
  --volume=/var/lib/docker/:/var/lib/docker:ro \
  --volume=/dev/disk/:/dev/disk:ro \
  --publish=8080:8080 \
  --detach=true \
  --name=cadvisor \
  --cpus 0.3 \
  --memory 200M \
  google/cadvisor:latest
EOF